package com.proj.hot.utils;

import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.proj.hot.GlobalParams;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by user on 17/8/31.
 */
public class TimeService extends Service {
    public static final String TAG = "MyService";


    Timer timer = new Timer();
    int sk=60;//真正情况为60s


   public static boolean TimeServiceIsLive=false;

    TimerTask task = new TimerTask() {
        @Override
        public void run() {


            if (!GlobalParams.IS_LOGIN){
                timer.cancel();
                TimeService.this.stopSelf();
                TimeServiceIsLive=false;
                return;
            }

            if (GlobalParams.user_Residualduration>-1){
                GlobalParams.user_Residualduration--;
            }



            Log.d(TAG, "GlobalParams.user_Residualduration="+GlobalParams.user_Residualduration);

            if (GlobalParams.user_Residualduration==15*sk
                    ||GlobalParams.user_Residualduration==5*sk
                    ||GlobalParams.user_Residualduration==0){
                Intent intent=new Intent();
                intent.setAction("payTips");
                intent.putExtra("msg","这是普通广播");
                sendBroadcast(intent);


            }


            if (GlobalParams.user_Residualduration==0){
                GlobalParams.user_paytype="2";
                timer.cancel();
                TimeService.this.stopSelf();
                TimeServiceIsLive=false;
            }



        }
    };


    @Override public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate() executed");

        timer.schedule(task, 1000, 1000);
        TimeServiceIsLive=true;

    }


    @Override public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");




        return super.onStartCommand(intent, flags, startId);



    }


    @Override public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() executed");}


    @Override public IBinder onBind(Intent intent) {
        return null; }
}
